package jmine.tec.web.wicket.upperCase;

import org.apache.wicket.Component;
import org.apache.wicket.model.IComponentInheritedModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.IWrapModel;

/**
 * {@link IComponentInheritedModel} that wraps the inner models on {@link UpperCaseModelWrapper} if needed.
 * 
 * @author takeshi
 * @param <T> the type
 */
public class UpperCaseInheritedModel<T> implements IComponentInheritedModel<T>, IWrapModel<T> {

    private final IComponentInheritedModel<T> delegate;

    /**
     * C'tor
     * 
     * @param delegate {@link IComponentInheritedModel}
     */
    public UpperCaseInheritedModel(IComponentInheritedModel<T> delegate) {
        super();
        this.delegate = delegate;
    }

    /**
     * {@inheritDoc}
     */
    public <W> IWrapModel<W> wrapOnInheritance(Component component) {
        IWrapModel<W> inherited = delegate.wrapOnInheritance(component);
        if (UpperCaseBeforeRenderListener.shouldReplaceModel(component)) {
            inherited = new UpperCaseModelWrapper<W>(inherited);
        }
        return inherited;
    }

    /**
     * {@inheritDoc}
     */
    public T getObject() {
        return delegate.getObject();
    }

    /**
     * {@inheritDoc}
     */
    public void setObject(T object) {
        delegate.setObject(object);
    }

    /**
     * {@inheritDoc}
     */
    public void detach() {
        this.delegate.detach();
    }

    /**
     * {@inheritDoc}
     */
    public IModel<?> getWrappedModel() {
        return delegate;
    }

}
